﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input.Test;
using System.Windows.Threading;
using WPFExcelReport.Helpers;
using System.Text;
namespace WPFExcelReport.Controls
{
    /// <summary>
    /// A control that shows a loading animation. xmlns:me="clr-namespace:WPFExcelReport" 
    /// </summary>
    public class TXT72 : TextBox
    {
        SendKeys SendKeys = new SendKeys();
        public void SendToUIThread(UIElement element, string text)
        {
            element.Dispatcher.BeginInvoke(
                new Action(() => { SendKeys.Send(element, text); }),
                DispatcherPriority.Input
            );
        }
        public TXT72()
        {
            FTAUTOTRIM = true;
        }
        #region ==PRE===========
        public static readonly DependencyProperty PREProperty = DependencyProperty.Register("PRE",
            typeof(string), typeof(TXT72),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPREChanged));
        public string PRE
        {
            get
            {
                return (string)GetValue(PREProperty);
            }
            set
            {
                SetValue(PREProperty, value);
            }
        }
        private static void OnPREChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TXT72;
            if (src != null)
            {
                src.PRE = (string)e.NewValue;
            }
        }
        #endregion
        #region ==IsAutoClear===========
        public static readonly DependencyProperty IsAutoClearProperty = DependencyProperty.Register("IsAutoClear",
            typeof(bool), typeof(TXT72),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsAutoClearChanged));
        public bool IsAutoClear
        {
            get
            {
                return (bool)GetValue(IsAutoClearProperty);
            }
            set
            {
                SetValue(IsAutoClearProperty, value);
            }
        }
        private static void OnIsAutoClearChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TXT72;
            if (src != null)
            {
                src.IsAutoClear = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsTrimEnd===========
        public static readonly DependencyProperty IsTrimEndProperty = DependencyProperty.Register("IsTrimEnd",
            typeof(bool), typeof(TXT72),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsTrimEndChanged));
        public bool IsTrimEnd
        {
            get
            {
                return (bool)GetValue(IsTrimEndProperty);
            }
            set
            {
                SetValue(IsTrimEndProperty, value);
            }
        }
        private static void OnIsTrimEndChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TXT72;
            if (src != null)
            {
                src.IsTrimEnd = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsAutoEnter===========
        public static readonly DependencyProperty IsAutoEnterProperty = DependencyProperty.Register("IsAutoEnter",
            typeof(bool), typeof(TXT72),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsAutoEnterChanged));
        public bool IsAutoEnter
        {
            get
            {
                return (bool)GetValue(IsAutoEnterProperty);
            }
            set
            {
                SetValue(IsAutoEnterProperty, value);
            }
        }
        private static void OnIsAutoEnterChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TXT72;
            if (src != null)
            {
                src.IsAutoEnter = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==FTAUTOTRIM===========
        public static readonly DependencyProperty FTAUTOTRIMProperty = DependencyProperty.Register("FTAUTOTRIM",
            typeof(bool), typeof(TXT72),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFTAUTOTRIMChanged));
        public bool FTAUTOTRIM
        {
            get
            {
                return (bool)GetValue(FTAUTOTRIMProperty);
            }
            set
            {
                SetValue(FTAUTOTRIMProperty, value);
            }
        }
        private static void OnFTAUTOTRIMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TXT72;
            if (src != null)
            {
                src.FTAUTOTRIM = (bool)e.NewValue;
            }
        }
        #endregion
        protected override void OnGotFocus(RoutedEventArgs e)
        {
            if (IsReadOnly)
                return;
            if (this.MaxLength > 0)
            {
                if(IsTrimEnd)
                {
                    this.Text = this.Text.TrimEnd();
                }
                else
                {
                    this.Text = this.Text.Trim();
                }
                
            }
            base.OnGotFocus(e);
            this.SelectAll();
            if (IsAutoClear)
            {
                this.Text = "";
            }
        }
        protected override void OnTextInput(System.Windows.Input.TextCompositionEventArgs e)
        {
            //if(e.Key!=System.Windows.Input.Key.Delete&&e.Key!=System.Windows.Input.Key.Back)
            {

                if (this.MaxLength > 0)
                {
                    int length = Encoding.Default.GetByteCount(Text.Trim());
                    if (IsTrimEnd)
                    {
                          length = Encoding.Default.GetByteCount(Text.TrimEnd());
                    }    
                    if (IsAutoEnter)
                    {
                        if (length >= this.MaxLength)
                        {
                            SendToUIThread(this, "{ENTER}");
                        }
                    }
                }
            }
            base.OnTextInput(e);
        }
        protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e)
        {
            //敲过回车后，自动补齐了空格，若想输入空格，先删除
            if (e.Key != System.Windows.Input.Key.Return || e.Key != System.Windows.Input.Key.Enter)
                return;

            if (this.MaxLength > 0)
            {
                int length = Encoding.Default.GetByteCount(Text.Trim());
                int netlength = Text.Trim().Length;
                if (IsTrimEnd)
                {
                    length = Encoding.Default.GetByteCount(Text.TrimEnd());
                   netlength = Text.TrimEnd().Length;
                }

                if (PRE.Length > 0)
                {
                    Text = Helper.Right(PRE + Text.Trim(), MaxLength);
                    if (IsTrimEnd)
                    {
                        Text = Helper.Right(PRE + Text.TrimEnd(), MaxLength);
                    }
                }
                else
                {
                    if (length < MaxLength)
                    {
                        if (IsTrimEnd)
                        {
                            Text = Text.TrimEnd().PadRight(MaxLength).Substring(0, MaxLength + (netlength - length));
                        }
                        else
                        {
                            Text = Text.Trim().PadRight(MaxLength).Substring(0, MaxLength + (netlength - length));
                        }

                    }
                    else
                    {
                        if (IsTrimEnd)
                        {
                            Text = Text.TrimEnd().PadRight(MaxLength).Substring(0, MaxLength + (netlength - length));
                        }
                        else
                        {
                            Text = Text.Trim().PadRight(MaxLength).Substring(0, MaxLength + (netlength - length));
                        }

                    }
                }
                this.SelectionStart = MaxLength;
            }

            base.OnKeyUp(e);
        }
        protected override void OnLostFocus(RoutedEventArgs e)
        {
            if (this.MaxLength > 0)
            {
                int length = Encoding.Default.GetByteCount(Text.Trim());
                int netlength = Text.Trim().Length;
                if (IsTrimEnd)
                {
                    length = Encoding.Default.GetByteCount(Text.TrimEnd());
                    netlength = Text.TrimEnd().Length;
                }

                if (length > MaxLength)
                    length = MaxLength;
                if (PRE.Length > 0)
                {
                    if (IsTrimEnd)
                    {
                        Text = Helper.Right(PRE + Text.TrimEnd(), MaxLength);
                    }
                    else
                    {
                        Text = Helper.Right(PRE + Text.Trim(), MaxLength);
                    }
                        
                }
                else
                {
                    if (length < MaxLength)
                    {
                        if (IsTrimEnd)
                        {
                            Text = Text.TrimEnd().PadRight(MaxLength).Substring(0, MaxLength + (netlength - length));
                        }
                        else
                        {
                            Text = Text.Trim().PadRight(MaxLength).Substring(0, MaxLength + (netlength - length));
                        }
                        
                    }
                    else
                    {
                        if (IsTrimEnd)
                        {
                            Text = Text.TrimEnd().PadRight(MaxLength).Substring(0, MaxLength + (netlength - length));
                        }
                        else
                        {
                            Text = Text.Trim().PadRight(MaxLength).Substring(0, MaxLength + (netlength - length)); 
                        }
                        
                    }
                }
            }
            base.OnLostFocus(e);
        }
    }
}
